package com.iqiao.common.util;

import com.iqiao.common.mybits.enums.DisplayedEnum;
import org.apache.commons.lang3.EnumUtils;

public class EnumUtil extends EnumUtils {

    public static <T extends Enum<T>> boolean isValidEnum(Class<T> enumClass, Integer enumValue) {
        if (enumValue == null) {
            return false;
        } else {
            try {
                valueOfEnum(enumClass, enumValue);
                return true;
            } catch (IllegalArgumentException var3) {
                return false;
            }
        }
    }

    public static <T extends Enum<T>> T valueOfEnum(Class<T> enumClass, Integer value) {
        if (value == null)
            throw  new IllegalArgumentException("DisplayedEnum value should not be null");
        if (enumClass.isAssignableFrom(DisplayedEnum.class))
            throw new IllegalArgumentException("illegal DisplayedEnum type");
        T[] enums = enumClass.getEnumConstants();
        for (T t: enums) {
            DisplayedEnum displayedEnum = (DisplayedEnum)t;
            if (displayedEnum.getValue().equals(value))
                return (T) displayedEnum;
        }
        throw new IllegalArgumentException("cannot parse integer: " + value + " to " + enumClass.getName());
    }
}
